package com.samp.base.jce.type01;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class SHADemo {
    public static String encrypt(String shaType, String info ) throws NoSuchAlgorithmException {
        MessageDigest md5 = MessageDigest.getInstance(shaType);
        byte[] srcBytes = info.getBytes();
        md5.update(srcBytes);
        byte[] resultBytes = md5.digest();
        return Base64.getEncoder().encodeToString(resultBytes);
    }

    public static void main(String[] args) {
        String[] shaTypes = {"SHA1","SHA-256","SHA-384","SHA-512"};
        for(String shaType: shaTypes){
            try {
                System.out.println("[张三]经" + shaType + "加密后为：" + encrypt(shaType, "张三"));
                System.out.println("[张三aaaefa123!@$!!@!@!(&&/.,,']经" + shaType
                            + "加密后为：" + encrypt(shaType, "张三aaaefa123!@$!!@!@!(&&/.,,'"));
            }catch (NoSuchAlgorithmException e){
                e.printStackTrace();
            }
        }
    }

}
